#include "app.h"
#include "dwt_delay.h"
#include "main.h"
#include <errno.h>
#include <stdio.h>
#include <sys/unistd.h> // STDOUT_FILENO, STDERR_FILENO

extern UART_HandleTypeDef huart1;

int _write(int file, char *ptr, int len)
{
	switch (file) {
	case STDOUT_FILENO: // stdout
		HAL_UART_Transmit_IT(&huart1, (uint8_t *)ptr, len);
		break;
	case STDERR_FILENO: // stderr
		// Send the string somewhere
		break;
	default:
		return -1;
	}
	return len;
}

void app_start(void)
{
	DWT_Init();

	printf("App start\r\n");

	for (uint8_t k = 0; k < 5; k++) {
		for (uint32_t i = 0; i < 100; i++) {
			DWT_Delay(1000);
		}
		HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
	}

	while (1) {
		HAL_Delay(2000);
		printf("Infinite Loop...\r\n");
		HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
	}
}